package com.kaizeli.website.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaizeli.website.mapper.TravelExpenseMapper;
import com.kaizeli.website.pojo.entity.TravelExpense;
import com.kaizeli.website.pojo.dto.TravelExpenseDTO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.TravelExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 差旅费用服务实现类
 */
@Service
public class TravelExpenseServiceImpl extends ServiceImpl<TravelExpenseMapper, TravelExpense> implements TravelExpenseService {

    @Autowired
    private TravelExpenseMapper travelExpenseMapper;

    @Override
    public Result<List<TravelExpenseDTO>> getTravelExpensesByAcceptanceId(Long acceptanceId) {
        try {
            List<TravelExpenseDTO> travelExpenses = travelExpenseMapper.selectByAcceptanceId(acceptanceId);
            return Result.success(travelExpenses);
        } catch (Exception e) {
            return Result.error(500, "查询差旅费用记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<TravelExpenseDTO>> getTravelExpensesByEmployeeId(Long employeeId) {
        try {
            List<TravelExpenseDTO> travelExpenses = travelExpenseMapper.selectByEmployeeId(employeeId);
            return Result.success(travelExpenses);
        } catch (Exception e) {
            return Result.error(500, "查询差旅费用记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<TravelExpenseDTO>> getTravelExpensesByProjectId(Long projectId) {
        try {
            List<TravelExpenseDTO> travelExpenses = travelExpenseMapper.selectByProjectId(projectId);
            return Result.success(travelExpenses);
        } catch (Exception e) {
            return Result.error(500, "查询差旅费用记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<TravelExpense> createTravelExpense(TravelExpense travelExpense) {
        try {
            travelExpense.setStatus("待审核");
            travelExpense.setCreateTime(LocalDateTime.now());
            travelExpense.setUpdateTime(LocalDateTime.now());
            save(travelExpense);
            return Result.success(travelExpense);
        } catch (Exception e) {
            return Result.error(500, "创建差旅费用记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<TravelExpense> updateTravelExpense(TravelExpense travelExpense) {
        try {
            travelExpense.setUpdateTime(LocalDateTime.now());
            updateById(travelExpense);
            return Result.success(travelExpense);
        } catch (Exception e) {
            return Result.error(500, "更新差旅费用记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<Void> reviewTravelExpense(Long id, String status) {
        try {
            TravelExpense travelExpense = getById(id);
            if (travelExpense == null) {
                return Result.error(404, "差旅费用记录不存在");
            }
            
            if (!"待审核".equals(travelExpense.getStatus())) {
                return Result.error(400, "差旅费用记录状态不正确，无法审核");
            }
            
            int result = travelExpenseMapper.updateStatus(id, status);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error(500, "审核差旅费用失败");
            }
        } catch (Exception e) {
            return Result.error(500, "审核差旅费用失败：" + e.getMessage());
        }
    }

    @Override
    public Result<BigDecimal> calculateTotalTravelExpense(Long acceptanceId) {
        try {
            BigDecimal totalTravelExpense = travelExpenseMapper.calculateTotalTravelExpense(acceptanceId);
            return Result.success(totalTravelExpense);
        } catch (Exception e) {
            return Result.error(500, "计算总差旅费用失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<Void> batchCreateTravelExpenses(List<TravelExpense> travelExpenses) {
        try {
            for (TravelExpense travelExpense : travelExpenses) {
                travelExpense.setStatus("待审核");
                travelExpense.setCreateTime(LocalDateTime.now());
                travelExpense.setUpdateTime(LocalDateTime.now());
            }
            
            saveBatch(travelExpenses);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "批量创建差旅费用记录失败：" + e.getMessage());
        }
    }
}
